package com.bigevent.controller;


import com.bigevent.pojo.Article;
import com.bigevent.pojo.PageBean;
import com.bigevent.pojo.Result;
import com.bigevent.service.ArticleServiece;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    /*@GetMapping("/list")
    public Result<String> list(*//*@RequestHeader(name = "Authorization") String token, HttpServletResponse response*//*) {
        // 验证token
        *//*try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            return Result.success("文章列表");
        } catch (Exception e) {
            // http响应状态码为401
            response.setStatus(401);
            return Result.error("未登录");
        }*//*
        return Result.success("文章列表");
    }*/
    @Autowired
    private ArticleServiece articleServiece;
    /*
    * 新增文章分类
    * */
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleServiece.add(article);
        return Result.success();
    }
    /*
    * 分页查询
    * */
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) Integer categoryId
    ){
        PageBean<Article> pb = articleServiece.list(pageNum,pageSize,state,categoryId);
        return Result.success(pb);
    }

    /*
    * 修改文章
    * */
    @PutMapping
    public Result update(@RequestBody Article article){
        articleServiece.update(article);
        return Result.success();
    }

    /*
    * 根据id删除文章
    * */
    @DeleteMapping
    public Result delete(Integer id){
        articleServiece.delete(id);
        return Result.success();
    }
}
